package com.xoa.service.equipment.impl;

import com.alibaba.fastjson.JSONArray;
import com.xoa.dao.equipment.*;
import com.xoa.model.equipment.*;
import com.xoa.service.equipment.LimsEquipEventPlanParaService;
import com.xoa.service.equipment.LimsEquipEventsPlanService;
import com.xoa.util.LimsJson;
import com.xoa.util.common.StringUtils;
 import com.xoa.util.page.PageParams;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @作者 杨岩林
 * @创建日期 9:352019/12/16
 * @类介绍
 */
@Service
public class LimsEquipEventsPlanServiceImpl implements LimsEquipEventsPlanService {

    @Autowired
    private LimsEquipEventsPlanMapper limsEquipEventsPlanMapper;
    @Autowired
    private LimsEquipEventPlanParaMapper limsEquipEventPlanParaMapper;
    @Autowired
    private EquipTypeMapper equipTypeMapper;
    @Autowired
    private LimsEquipEventlogMapper limsEquipEventlogMapper;
    @Autowired
    private LimsEquipEventPlanParaService limsEquipEventPlanParaService;
    @Autowired
    private EquipCapyMapper equipCapyMapper;

    @Override
    public LimsJson add(LimsEquipEventsPlan limsEquipEventsPlan1,String json,String paraJson) {
        LimsJson limsJson=new LimsJson(1,"err");
        int x=0;
        int b=0;
        int c=0;
        int d=0;
        LimsEquipEventsPlan limsEquipEventsPlan=this.updateDate(limsEquipEventsPlan1);
        if(!StringUtils.checkNull(json)&&limsEquipEventsPlan!=null){
            List<Equipment> equipmentList= JSONArray.parseArray(json,Equipment.class);
           // List<Equipment> equipmentList = (List<Equipment>) JSONArray.toCollection(jsonarray, Equipment.class);
            for(Equipment equipment:equipmentList){
                x++;
                limsEquipEventsPlan.setEventId(null);
                limsEquipEventsPlan.setEquipId(equipment.getEquipId());
                limsEquipEventsPlan.setEquipName(equipment.getEquipName());
                limsEquipEventsPlan.setEquipNo(equipment.getEquipNo());
                limsEquipEventsPlan.setEquipStatus(equipment.getEquipStatusCode());
                limsEquipEventsPlan.setFactoryNo(equipment.getFactoryNo());
                limsEquipEventsPlan.setModelNo(equipment.getModelNo());
                int result=limsEquipEventsPlanMapper.insertSelective(limsEquipEventsPlan);
                if(result>0){
                    b++;
                    if(!StringUtils.checkNull(paraJson) && !"[{}]".equals(paraJson)){
                        List<LimsEquipEventPlanPara> limsEquipEventPlanParas = JSONArray.parseArray(paraJson, LimsEquipEventPlanPara.class);
                       // List<LimsEquipEventPlanPara> limsEquipEventPlanParaList = (List<LimsEquipEventPlanPara>) JSONArray.toCollection(jsonarray1, LimsEquipEventPlanPara.class);
                        for(LimsEquipEventPlanPara limsEquipEventPlanPara:limsEquipEventPlanParas){
                            c++;
                            limsEquipEventPlanPara.setEventId(limsEquipEventsPlan.getEventId());
                            int result1=limsEquipEventPlanParaMapper.insertSelective(limsEquipEventPlanPara);
                            if(result1>0){
                                d++;
                            }
                        }
                    }

                }

            }
            if(x==b && c==d){
                limsJson.setMsg("新增成功");
                limsJson.setCode(0);
            }
        }else{
            limsJson.setMsg("请先添加设备");
            limsJson.setCode(1);
        }

        return limsJson;
    }

    @Override
    public LimsJson delete(String ids) {
        LimsJson limsJson=new LimsJson(1,"err");
        String[] idArray=ids.split(",");
        int result=limsEquipEventsPlanMapper.deleteByIds(idArray);
        if(result>0){
            limsJson.setMsg("删除成功");
            limsJson.setCode(0);
        }
        return limsJson;
    }


    @Override
    public LimsJson queryAll(LimsEquipEventsPlan limsEquipEventsPlan, Integer page, Integer limit) {
        LimsJson limsJson=new LimsJson(1,"err");
        Map map=new HashMap();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("limsEquipEventsPlan",limsEquipEventsPlan);
        List<EquipType> equipTypeList = equipTypeMapper.selectChildEquipType(limsEquipEventsPlan.getEquipTypeId());
        List<Integer> list=new ArrayList<>();
        list.add(limsEquipEventsPlan.getEquipTypeId());
        if(equipTypeList!=null && equipTypeList.size()>0) {
            for (EquipType equipType : equipTypeList) {
                list.add(equipType.getId());
            }
        }
        map.put("list",list);
        List<LimsEquipEventsPlan> limsEquipEventsPlans=limsEquipEventsPlanMapper.selectAll(map);
        map.remove("page");
        Integer count=limsEquipEventsPlanMapper.selectCount(map);
        if(limsEquipEventsPlans!=null && limsEquipEventsPlans.size()>0){
            for(LimsEquipEventsPlan limsEquipEventsPlan1:limsEquipEventsPlans){
                String s1=getCurrentTime(new Date());
                String s2=getCurrentTime(limsEquipEventsPlan1.getNestExeTime());
                Integer a=timeCompare(s1,s2);
                if(a.equals(1) || a.equals(0)) {
                    limsEquipEventsPlan1.setEventStatus("已到期,未执行");
                }else{
                    limsEquipEventsPlan1.setEventStatus("未到期");
                }

            }
            limsJson.setMsg("查询成功");
            limsJson.setCode(0);
            limsJson.setData(limsEquipEventsPlans);
            limsJson.setCount(count);
        }else{
            limsJson.setMsg("无数据");
            limsJson.setCode(0);
        }
        return limsJson;
    }

    @Override
    public LimsJson queryOne(Integer id) {
        LimsJson limsJson=new LimsJson(1,"err");
        LimsEquipEventsPlan limsEquipEventsPlan=limsEquipEventsPlanMapper.selectByPrimaryKey(id);
        if(limsEquipEventsPlan!=null){
            if(limsEquipEventsPlan.getEquipStatus()!=null){
                EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(limsEquipEventsPlan.getEquipStatus(),null);
                if(equipCapy!=null){
                    limsEquipEventsPlan.setEquipStatusName(equipCapy.getCodeTypeName());
                }
            }
            limsJson.setCode(0);
            limsJson.setMsg("查询成功");
            limsJson.setObject(limsEquipEventsPlan);
        }
        return limsJson;
    }

    @Override
    public LimsJson updateAll(String ids, LimsEquipEventsPlan limsEquipEventsPlan1,String json) {
        LimsJson limsJson=new LimsJson(1,"err");
        LimsEquipEventsPlan limsEquipEventsPlan=this.updateDate(limsEquipEventsPlan1);
        String[] idArray=ids.split(",");
        Map map=new HashMap();
        map.put("ids",idArray);
        map.put("limsEquipEventsPlan",limsEquipEventsPlan);
        if(StringUtils.checkNull(json)){
            int a1=limsEquipEventsPlanMapper.updateByIds(map);
            if(a1>0){
                limsJson.setMsg("修改成功");
                limsJson.setCode(0);
            }
        }else{
            int b=limsEquipEventsPlanMapper.updateByIds(map);
            if(b>0){
            limsJson=limsEquipEventPlanParaService.addOrUpdate(json);
            }
        }

        return limsJson;
    }

    //获取时间字符串
    public static String getCurrentTime(Date date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }
    //比较时间大小
    public static int timeCompare(String t1,String t2){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1=Calendar.getInstance();
        Calendar c2=Calendar.getInstance();
        try {
            c1.setTime(formatter.parse(t1));
            c2.setTime(formatter.parse(t2));
        } catch (Exception e) {
            e.printStackTrace();
        }
        int result=c1.compareTo(c2);
        return result;
    }

    @Override
    public LimsJson queryEventParas(Integer eventId) {
        LimsJson limsJson=new LimsJson(1,"err");
        List<LimsEquipEventPlanPara> limsEquipEventPlanParas=limsEquipEventPlanParaMapper.queryByEventId(eventId);
        if(limsEquipEventPlanParas!=null && limsEquipEventPlanParas.size()>0){
            limsJson.setCode(0);
            limsJson.setMsg("查询成功");
            limsJson.setData(limsEquipEventPlanParas);
        }else{
            limsJson.setMsg("无数据");
            limsJson.setCode(0);
        }
        return limsJson;
    }

    //计算下次到期日期
    public LimsEquipEventsPlan updateDate(LimsEquipEventsPlan limsEquipEventsPlan){
        if(limsEquipEventsPlan!=null){
            String a=limsEquipEventsPlan.getFrequency();
            //设置下次到期时间为上次执行时间加频率
            Date lastExeTime=limsEquipEventsPlan.getLastExeTime();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(lastExeTime.getTime());
            int currenYear = calendar.get(Calendar.YEAR);
            int currenMonth = calendar.get(Calendar.MONTH);
            int currenDay = calendar.get(Calendar.DAY_OF_MONTH);
            switch (a){
                case "1":
                    calendar.set(Calendar.YEAR, currenYear + 2);
                    java.sql.Date newDate = new java.sql.Date(calendar.getTimeInMillis());
                    limsEquipEventsPlan.setNestExeTime(newDate);
                    break;
                case "2":
                    calendar.set(Calendar.YEAR, currenYear + 1);
                    java.sql.Date newDate1 = new java.sql.Date(calendar.getTimeInMillis());
                    limsEquipEventsPlan.setNestExeTime(newDate1);
                    break;
                case "3":
                    calendar.set(Calendar.MONTH, currenMonth + 1);
                    java.sql.Date newDate2 = new java.sql.Date(calendar.getTimeInMillis());
                    limsEquipEventsPlan.setNestExeTime(newDate2);
                    break;
                case "4":
                    calendar.set(Calendar.MONTH, currenMonth + 3);
                    java.sql.Date newDate3 = new java.sql.Date(calendar.getTimeInMillis());
                    limsEquipEventsPlan.setNestExeTime(newDate3);
                    break;
                case "5":
                    calendar.set(Calendar.DAY_OF_MONTH, currenDay + 7);
                    java.sql.Date newDate4 = new java.sql.Date(calendar.getTimeInMillis());
                    limsEquipEventsPlan.setNestExeTime(newDate4);
                    break;
                case "6":
                    calendar.set(Calendar.DAY_OF_MONTH, currenDay + 1);
                    java.sql.Date newDate5 = new java.sql.Date(calendar.getTimeInMillis());
                    limsEquipEventsPlan.setNestExeTime(newDate5);
                default:
                    break;
            }
        }
        return limsEquipEventsPlan;
    }


}
